package top.likeqc.leetcode.editor.cn;

/**
 * @see <a href="https://leetcode.cn/problems/convert-to-base-2">1017. 负二进制转换</a>
 */
public class ConvertToBase2 {
    public static void main(String[] args) {
        Solution solution = new ConvertToBase2().new Solution();
        System.out.println(solution.baseNeg2(2));
        System.out.println(solution.baseNeg2(3));
        System.out.println(solution.baseNeg2(4));
    }
    // leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public String baseNeg2(int n) {
            if (Math.abs(n) <= 1) {
                return String.valueOf(Math.abs(n));
            }
            StringBuilder sb = new StringBuilder();
            while (n != 0) {
                int t = n & 1;
                n -= t;
                sb.append(t);
                n /= -2;
            }

            return sb.reverse().toString();
        }
    }
    // leetcode submit region end(Prohibit modification and deletion)

}
